<template>
  <el-form class="search-form" :inline="isInline" :model="modelValue" @submit.prevent="handleSearch">
    <div class="search-form-content">
      <div class="search-form-fields">
        <slot></slot>
      </div>
      <div class="search-form-actions">
        <el-form-item>
          <el-button type="primary" @click="handleSearch">
            <el-icon><Search /></el-icon>
            搜索
          </el-button>
          <el-button @click="handleReset">
            <el-icon><Refresh /></el-icon>
            重置
          </el-button>
        </el-form-item>
      </div>
    </div>
  </el-form>
</template>

<script setup lang="ts">
import { ref, onMounted, onUnmounted } from 'vue'
import { Search, Refresh } from '@element-plus/icons-vue'

interface Props {
  modelValue: Record<string, any>
}

interface Emits {
  (e: 'update:modelValue', value: Record<string, any>): void
  (e: 'search', value: Record<string, any>): void
  (e: 'reset'): void
}

const props = defineProps<Props>()
const emit = defineEmits<Emits>()

// 响应式布局控制
const isInline = ref(true)

// 检查屏幕宽度
const checkScreenSize = () => {
  isInline.value = window.innerWidth > 768
}

// 监听窗口大小变化
const handleResize = () => {
  checkScreenSize()
}

onMounted(() => {
  checkScreenSize()
  window.addEventListener('resize', handleResize)
})

onUnmounted(() => {
  window.removeEventListener('resize', handleResize)
})

const handleSearch = () => {
  emit('search', props.modelValue)
}

const handleReset = () => {
  const resetValue: Record<string, any> = {}
  Object.keys(props.modelValue).forEach(key => {
    resetValue[key] = ''
  })
  emit('update:modelValue', resetValue)
  emit('reset')
}
</script>

<style lang="scss" scoped>
.search-form {
  padding: 20px;
  background: #fff;
  border-radius: 4px;
  margin-bottom: 20px;
  box-shadow: 0 1px 2px rgba(0, 0, 0, 0.05);

  .search-form-content {
    display: flex;
    flex-wrap: wrap;
    gap: 16px;
    align-items: flex-end;

    .search-form-fields {
      display: flex;
      flex-wrap: wrap;
      gap: 16px;
      flex: 1;
      min-width: 0;
    }

    .search-form-actions {
      flex-shrink: 0;
    }
  }

  // 内联模式样式
  &.el-form--inline {
    :deep(.el-form-item) {
      margin-bottom: 0;
      margin-right: 0;
    }
  }

  // 非内联模式样式
  &:not(.el-form--inline) {
    .search-form-content {
      flex-direction: column;
      align-items: stretch;

      .search-form-fields {
        :deep(.el-form-item) {
          margin-bottom: 16px;
          margin-right: 0;
          width: 100%;

          .el-form-item__content {
            width: 100%;

            .el-input,
            .el-select,
            .el-date-editor {
              width: 100%;
            }
          }
        }
      }

      .search-form-actions {
        :deep(.el-form-item) {
          margin-bottom: 0;
          text-align: center;

          .el-button {
            margin-right: 8px;

            &:last-child {
              margin-right: 0;
            }
          }
        }
      }
    }
  }

  // 响应式设计
  @media (max-width: 1200px) {
    .search-form-content {
      .search-form-fields {
        :deep(.el-form-item) {
          min-width: 200px;
        }
      }
    }
  }

  @media (max-width: 768px) {
    padding: 16px;

    .search-form-content {
      gap: 12px;

      .search-form-fields {
        gap: 12px;

        :deep(.el-form-item) {
          min-width: 0;
          width: 100%;

          .el-form-item__label {
            width: auto !important;
            margin-bottom: 4px;
          }

          .el-form-item__content {
            margin-left: 0 !important;
          }
        }
      }

      .search-form-actions {
        :deep(.el-form-item) {
          .el-button {
            width: 100%;
            margin-bottom: 8px;
            margin-right: 0;

            &:last-child {
              margin-bottom: 0;
            }
          }
        }
      }
    }
  }

  @media (max-width: 480px) {
    padding: 12px;

    .search-form-content {
      .search-form-actions {
        :deep(.el-form-item) {
          display: flex;
          flex-direction: column;
          gap: 8px;
        }
      }
    }
  }
}
</style>